package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.AnnouncementListResponse;
import com.molyfun.parents.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 */
public class AnnouncementView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.announcement_content)
    TextView mAnnouncementContent;
    @InjectView(R.id.user)
    TextView mUser;
    @InjectView(R.id.time)
    TextView mTime;
    @InjectView(R.id.line)
    View mLine;
    @InjectView(R.id.announcement_title)
    TextView mAnnouncementTitle;

    private Context context;
    private AnnouncementListResponse.AnnouncementBean mAnnouncementBean;

    public AnnouncementView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
        init(context, attrs);
    }

    public AnnouncementView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AnnouncementView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_announcement, this);
        ButterKnife.inject(this);
        setOnClickListener(this);
    }

    public void initData(AnnouncementListResponse.AnnouncementBean announcementBean) {
        initData(announcementBean, false);
    }

    public void initData(AnnouncementListResponse.AnnouncementBean announcementBean, boolean isLast) {
        if (announcementBean == null) {
            return;
        }
        mAnnouncementBean = announcementBean;
        mAnnouncementContent.setText(StringUtils.getSpltString(mAnnouncementBean.getNotice(), 36));
        mAnnouncementTitle.setText(mAnnouncementBean.getTitle());
        mUser.setText("发布人:" + mAnnouncementBean.getUsername());
        mTime.setText("时间:" + StringUtils.formatDate(mAnnouncementBean.getCreatedatetime()));
        if(isLast){
            mLine.setVisibility(View.GONE);
        }else{
            mLine.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                AnnouncementDetailActivity.startActivity(context, mAnnouncementBean);
                break;
        }
    }
}